package org.ix.dynamicapis.service.impl;

import org.ix.dynamicapis.exception.DmpDataNotFoundException;
import org.ix.dynamicapis.model.PageList;
import org.ix.dynamicapis.utils.ObjectUtils;
import org.ix.dynamicapis.constant.DeletedConstant;
import org.ix.dynamicapis.presentation.condition.ApiSystemSelectCondition;
import org.ix.dynamicapis.presentation.condition.ApiSystemUpdateCondition;
import org.ix.dynamicapis.presentation.entity.ApiSystemEntity;
import org.ix.dynamicapis.presentation.entity.ApiSystemEntityExt;
import org.ix.dynamicapis.presentation.mapper.ApiSystemMapper;
import org.ix.dynamicapis.model.request.ApiSystemAddRequest;
import org.ix.dynamicapis.model.request.ApiSystemPatchRequest;
import org.ix.dynamicapis.model.request.ApiSystemQueryRequest;
import org.ix.dynamicapis.model.request.ApiSystemSaveRequest;
import org.ix.dynamicapis.model.response.ApiSystemDetailResponse;
import org.ix.dynamicapis.model.response.ApiSystemListItemResponse;
import org.ix.dynamicapis.service.ApiSystemService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

/**
 * 接口业务系统业务逻辑
 *
 * @author 郗世豪（rarexixi@gmail.com）
 */
@Service("apiSystemService")
@Transactional
public class ApiSystemServiceImpl implements ApiSystemService {

    final ApiSystemMapper apiSystemMapper;

    @Autowired
    public ApiSystemServiceImpl(ApiSystemMapper apiSystemMapper) {
        this.apiSystemMapper = apiSystemMapper;
    }

    /**
     * 添加接口业务系统
     *
     * @param addRequest 接口业务系统
     * @return 受影响的行数
     * @author 郗世豪（rarexixi@gmail.com）
     */
    @Override
    public ApiSystemDetailResponse add(ApiSystemAddRequest addRequest) {
        ApiSystemEntity entity = ObjectUtils.copy(addRequest, ApiSystemEntity.class);
        apiSystemMapper.insert(entity);
        return getById(entity.getId());
    }

    /**
     * 批量添加接口业务系统
     *
     * @param list 接口业务系统列表
     * @return 受影响的行数
     * @author 郗世豪（rarexixi@gmail.com）
     */
    @Override
    public int batchAdd(Collection<ApiSystemAddRequest> list) {
        List<ApiSystemEntity> entityList = ObjectUtils.copy(list, ApiSystemEntity.class);
        return apiSystemMapper.batchInsert(entityList);
    }

    /**
     * 删除接口业务系统
     *
     * @param patchRequest 更新条件请求
     * @return 受影响的行数
     * @author 郗世豪（rarexixi@gmail.com）
     */
    @Override
    public int delete(ApiSystemPatchRequest patchRequest) {
        ApiSystemUpdateCondition condition = ObjectUtils.copy(patchRequest, ApiSystemUpdateCondition.class);
        return apiSystemMapper.delete(condition);
    }

    /**
     * 禁用接口业务系统
     *
     * @param patchRequest 更新条件请求
     * @return 受影响的行数
     * @author 郗世豪（rarexixi@gmail.com）
     */
    @Override
    public int disable(ApiSystemPatchRequest patchRequest) {
        ApiSystemUpdateCondition condition = ObjectUtils.copy(patchRequest, ApiSystemUpdateCondition.class);
        ApiSystemEntity entity = ObjectUtils.copy(patchRequest, ApiSystemEntity.class, "id");
        entity.setDeleted(DeletedConstant.INVALID);
        return apiSystemMapper.update(entity, condition);
    }

    /**
     * 启用接口业务系统
     *
     * @param patchRequest 更新条件请求
     * @return 受影响的行数
     * @author 郗世豪（rarexixi@gmail.com）
     */
    @Override
    public int enable(ApiSystemPatchRequest patchRequest) {
        ApiSystemUpdateCondition condition = ObjectUtils.copy(patchRequest, ApiSystemUpdateCondition.class);
        ApiSystemEntity entity = ObjectUtils.copy(patchRequest, ApiSystemEntity.class, "id");
        entity.setDeleted(DeletedConstant.VALID);
        return apiSystemMapper.update(entity, condition);
    }

    /**
     * 根据接口ID更新接口业务系统
     *
     * @param saveRequest 保存接口业务系统请求实体
     * @return 更新后的接口业务系统详情
     * @author 郗世豪（rarexixi@gmail.com）
     */
    @Override
    public ApiSystemDetailResponse updateById(ApiSystemSaveRequest saveRequest) {
        ApiSystemUpdateCondition condition = new ApiSystemUpdateCondition();
        condition.setId(saveRequest.getId());
        ApiSystemEntity entity = ObjectUtils.copy(saveRequest, ApiSystemEntity.class);
        apiSystemMapper.update(entity, condition);
        return getById(saveRequest.getId());
    }

    /**
     * 根据接口ID获取接口业务系统详情
     *
     * @param id 接口ID
     * @return 接口业务系统详情
     * @author 郗世豪（rarexixi@gmail.com）
     */
    @Override
    @Transactional(readOnly = true)
    public ApiSystemDetailResponse getById(Integer id) {
        ApiSystemEntityExt entity = apiSystemMapper.detail(id);
        if (entity == null) {
            throw new DmpDataNotFoundException("接口业务系统不存在");
        }
        return ObjectUtils.copy(entity, ApiSystemDetailResponse.class);
    }

    /**
     * 获取接口业务系统列表
     *
     * @param queryRequest 搜索条件
     * @return 符合条件的接口业务系统列表
     */
    @Override
    @Transactional(readOnly = true)
    public List<ApiSystemListItemResponse> getList(ApiSystemQueryRequest queryRequest) {
        ApiSystemSelectCondition condition = ObjectUtils.copy(queryRequest, ApiSystemSelectCondition.class);
        List<ApiSystemEntityExt> list = apiSystemMapper.select(condition);
        return ObjectUtils.copy(list, ApiSystemListItemResponse.class);
    }

    /**
     * 分页获取接口业务系统列表
     *
     * @param queryRequest 搜索条件
     * @param pageNum      页码
     * @param pageSize     分页大小
     * @return 符合条件的接口业务系统分页列表
     */
    @Override
    @Transactional(readOnly = true)
    public PageList<ApiSystemListItemResponse> getPageList(ApiSystemQueryRequest queryRequest, Integer pageNum, Integer pageSize) {

        ApiSystemSelectCondition condition = ObjectUtils.copy(queryRequest, ApiSystemSelectCondition.class);
        PageInfo<ApiSystemEntityExt> pageInfo = PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> apiSystemMapper.select(condition));

        List<ApiSystemListItemResponse> list = ObjectUtils.copy(pageInfo.getList(), ApiSystemListItemResponse.class);
        return new PageList<>(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal(), list);
    }
}
